package com.tstudy.laoshibang.base;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

public class BaseOnScrollListener implements OnScrollListener{
	LoadMoreCallBack mLoadMoreCallBack;
	FirstPositionCallBack mFirstPositionCallBack;
	public boolean mIsEnd;
	public BaseOnScrollListener(LoadMoreCallBack loadMoreCallBack){
		mLoadMoreCallBack = loadMoreCallBack;
	}
	public interface LoadMoreCallBack{
		public void loadMore();
	}
	public interface FirstPositionCallBack{
		public void firstPosition(int firstPosition);
	}
	public void setFirstPositionCallBack(FirstPositionCallBack firstPositionCallBack){
		mFirstPositionCallBack = firstPositionCallBack;
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
		View lastChild = view.getChildAt(view.getChildCount()-1);
//		LogUtil.d("f==" + firstVisibleItem + ",v==" + visibleItemCount + ",t==" + totalItemCount);
		if(lastChild != null && firstVisibleItem + visibleItemCount == totalItemCount && lastChild.getBottom()>= view.getBottom() && !mIsEnd){
			mLoadMoreCallBack.loadMore();
		}
		if(mFirstPositionCallBack != null){
			mFirstPositionCallBack.firstPosition(firstVisibleItem);
		}
	}
}
